/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package be.fedict.hsm.model.security;

import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.security.jacc.PolicyContext;
import javax.security.jacc.PolicyContextException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import be.fedict.hsm.entity.SecurityAuditEntity;
import be.fedict.hsm.entity.SecurityEvent;

@Stateless
@SecurityFunction("SF.AUDIT")
public class SecurityAuditGeneratorBean {

	private static final Log LOG = LogFactory
			.getLog(SecurityAuditGeneratorBean.class);

	@PersistenceContext
	private EntityManager entityManager;

	public void webServiceAuthenticationError() {
		LOG.debug("web service authentication error");
		String hostIdentifier = getHostIdentifier();
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_WS, hostIdentifier, null, false);
		this.entityManager.persist(securityAuditEntity);
	}

	private String getHostIdentifier() {
		HttpServletRequest httpServletRequest;
		try {
			httpServletRequest = (HttpServletRequest) PolicyContext
					.getContext("javax.servlet.http.HttpServletRequest");
		} catch (PolicyContextException e) {
			LOG.debug("JACC error: " + e.getMessage());
			return null;
		}
		String hostIdentifier = httpServletRequest.getRemoteHost();
		return hostIdentifier;
	}

	private String getSubjectIdentifier(X509Certificate subjectCertificate) {
		try {
			String subjectIdentifier = getSubjectIdentifier(subjectCertificate
					.getEncoded());
			return subjectIdentifier;
		} catch (CertificateEncodingException e) {
			LOG.debug("X509 encoding error: " + e.getMessage());
			return null;
		}
	}

	private String getSubjectIdentifier(byte[] encodedSubjectCertificate) {
		String subjectIdentifier = DigestUtils
				.sha1Hex(encodedSubjectCertificate);
		return subjectIdentifier;
	}

	public void webServiceAuthenticationError(X509Certificate subjectCertificate) {
		String hostIdentifier = getHostIdentifier();
		String subjectIdentifier = getSubjectIdentifier(subjectCertificate);
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_WS, hostIdentifier, subjectIdentifier,
				false);
		this.entityManager.persist(securityAuditEntity);
	}

	public void webServiceAuthenticationError(byte[] encodedSubjectCertificate) {
		String hostIdentifier = getHostIdentifier();
		String subjectIdentifier = getSubjectIdentifier(encodedSubjectCertificate);
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_WS, hostIdentifier, subjectIdentifier,
				false);
		this.entityManager.persist(securityAuditEntity);
	}

	public void webServiceAuthenticationError(String subjectIdentifier) {
		String hostIdentifier = getHostIdentifier();
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_WS, hostIdentifier, subjectIdentifier,
				false);
		this.entityManager.persist(securityAuditEntity);
	}

	public void adminAuthenticationError(String subjectIdentifier) {
		String hostIdentifier = getHostIdentifier();
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_ADMIN, hostIdentifier, subjectIdentifier,
				false);
		this.entityManager.persist(securityAuditEntity);
	}

	public void adminAuthenticationError(X509Certificate clientCertificate) {
		String subjectIdentifier = getSubjectIdentifier(clientCertificate);
		String hostIdentifier = getHostIdentifier();
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_ADMIN, hostIdentifier, subjectIdentifier,
				false);
		this.entityManager.persist(securityAuditEntity);
	}

	public void adminAuthenticationError() {
		String hostIdentifier = getHostIdentifier();
		SecurityAuditEntity securityAuditEntity = new SecurityAuditEntity(
				SecurityEvent.AUTHN_ADMIN, hostIdentifier, null, false);
		this.entityManager.persist(securityAuditEntity);
	}
}
